<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>添加用户</title>
 	<!-- 引入Css文件 -->
	<jsp:include page="/common/index_css.jsp"></jsp:include>
	<!-- 引入js文件 -->
	<jsp:include page="/common/index_js.jsp"></jsp:include>
	<!-- 表单验证控件 -->
	<link rel="stylesheet" href="${pageContext.request.contextPath}/arsset/validation/css/validationEngine.jquery.css">
	<script src="${pageContext.request.contextPath}/arsset/validation/jquery.validationEngine-zh_CN.js"></script>
	<script src="${pageContext.request.contextPath}/arsset/validation/js/jquery.validationEngine.js"></script>
 	<script type="text/javascript">
 		jQuery(document).ready(function() {
			jQuery("#userForm").validationEngine({});
			loadRole(function (){
				/*查看url参数加载当前要修改的用户*/
				var parm = getQueryString("userId");
				if(parm){get(parm);}				//如果修改加载用户
			});
		});
 	</script>
  </head>
  <body>
  	<!-- 用户表单 -->
  	<div class="col-md-12 graphs">
	   <div class="xs">
  	    <div class="well1 white">
        <form class="form-floating" id="userForm" >
          <fieldset>
          	<input type="hidden" id="userId" value=""/><!-- 用户隐藏id -->
            <div class="form-group  col-sm-12">
              <label class="control-label">用户名*(6-20位)</label>
              <input type="text" class="form-control1 validate[required,ajax[ajaxRegisterCheckUserName],minSize[6],maxSize[20]]" name="userName" value="" id="userName">
            </div>
            <div class="form-group  col-sm-6">
              <label class="control-label">姓氏*</label>
              <input type="text" class="form-control1 validate[required,maxSize[6]]" value="" id="lastName" >
            </div>
            <div class="form-group  col-sm-6">
              <label class="control-label">名字*</label>
              <input type="text" class="form-control1 validate[required,maxSize[6]]" value="" id="firstName" >
            </div>
            <div class="form-group  col-sm-12">
              <label class="control-label">地址*</label>
              <input type="text" class="form-control1 validate[required,maxSize[100]]" value="" id="address">
            </div>
            <div class="form-group col-sm-6">
              <label class="control-label">(可选项)添加角色</label>
              <input type="text" class="form-control1" id="roleName">
            </div>
            <div class="form-group col-sm-6">
             	<button type="button" onclick="dynamicAddRole();" class="btn btn-info btn-block" style="margin-top: 23.4333px; height: 40px;">确定添加</button>
            </div>
            <div class="form-group  col-sm-12">
              <label class="control-label">选择角色*</label>
              <select class="form-control1 validate[required]" id="role">
              </select>
            </div>
            <div class="form-group col-sm-6">
              <button type="button" onclick="saveUser();" class="btn btn-default btn-block" style="height: 40px;background-color: 376956;color: white;">提交</button>
            </div>
            <div class="form-group col-sm-6">
              <button type="button" onclick="resetUserForm();" class="btn btn-default btn-block" style="height: 40px;background-color: 495A80;color: white;">重置</button>
            </div>
          </fieldset>
        </form>
      </div>
    </div>
   </div>
   	<script type="text/javascript">
   	/*修改时候从后台获取实体*/
   	function get(userId){
   		var ii = layer.load(2,{//设置遮罩层
		 	 shade: [0.3] 
		});
   		$.post("${pageContext.request.contextPath}/user/user_get.action",{
   			"userId":userId
   		},function(res){
   			if(res.success){
 			$('#userId').val(res.user.userId);
   			$('#userName').val(res.user.userName);
   			$('#userName').attr({"disabled":true});
   			$('#lastName').val(res.user.lastName);
   			$('#firstName').val(res.user.firstName);
   			$('#address').val(res.user.address);
   			$('#role').val(res.user.role.roleId);
   			}
   			//关闭遮罩层
			layer.close(ii);
   		},"json");
   	}
   	/*保存用户*/
   	function saveUser(){
	   	var ii = layer.load(2,{//设置遮罩层
		 	 shade: [0.3] 
		});
   		$.post("${pageContext.request.contextPath}/user/user_save.action",{
   			"user.userId":$('#userId').val(),
   			"user.userName":$('#userName').val(),
   			"user.lastName":$('#lastName').val(),
   			"user.firstName":$('#firstName').val(),
   			"user.address":$('#address').val(),
   			"roleId":$('#role').val()
   		},function(res){
   			if(res.success){
   				mes("保存成功");
   				parent.loadItems({"pageBean.page":parent.$("#page").val(),"pageBean.pageSize":10});
   				var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
  				parent.layer.close(index);
   			}
   			else{
   				mes("保存失败");
   			}
   			//关闭遮罩层
			layer.close(ii);
   		},"json");
   		
   	}
   	/*从服务器加载角色*/
   	function loadRole(retFunction){
		var ii = layer.load(2,{//设置遮罩层
	 	 shade: [0.3] 
		});
   		$.post("${pageContext.request.contextPath}/role/role_allList.action",{},function(res){
   			if(res.success){
   				var html = "";
  				for(var i = 0;i < res.list.length;i++){
  					var role = res.list[i];
  					if(role.roleName != "社区服务者")	//社区服务者一定要有一个具体的对象，所以总的不展示
  					html += "<option value=\""+role.roleId+"\">"+role.roleName+"</option>";
  				}
  				$("#role").html(html);
   			}
   			if(retFunction) retFunction();
   			//关闭遮罩层
   			layer.close(ii);
   		},"json");
   	}
   	/*动态添加角色--一定是社区服务者*/
   	function dynamicAddRole(){
   		if($('#roleName').val() != ""){
   			var ii = layer.load(2,{//设置遮罩层
		 	 shade: [0.3] 
			});
	   		$.post("${pageContext.request.contextPath}/role/role_save.action",{
	   			"role.roleName": $('#roleName').val()
	   		},function(res){
	   			if(res.success){
	   				loadRole();		//重载角色列表
	   				mes("添加成功!");
	   			}else{
	   				mes("添加失败!再试一次!");
	   			}
	   		//关闭遮罩层
   			layer.close(ii);
	   		},"json");
   		}else{
   			mes("添加时候必须填写社区服务者名称!");
   		}
   	}
   	/*重置用户填写表单*/
   	function resetUserForm(){
		var parm = getQueryString("userId");//查看url参数加载当前要修改的用户
		if(parm){get(parm);}				//如果修改加载用户
		else{$("#userForm").reset();}
   	}
   	</script>
  </body>
</html>
